/* Operations that deal with the reviewing a product */
$(function(){
   
    var $communityRating = $('#community-rating');
    var $customerRating = $('.customer-rating');
    var $communityRatingWidget = $communityRating.find('input.star');
    
    var communityRating = $communityRating.data('community-rating') * 4 - 1;
    
    communityRating = Math.round(communityRating); //round to nearest whole number to be compatible with star widgit

    $communityRatingWidget.rating('select', communityRating).rating('disable');
    
    $customerRating.each(function() {
        var customerRating = $(this).data('customer-rating') - 1;
        $(this).find('input.star').rating('select', customerRating).rating('disable');
    });
    
    
    
    // Bind all links with class 'create-review' to open the review modal
    $('body').on('click', 'a.create-review', function() {
    	
    	var url = $(this).attr('href');
    	$('.ratings-place').load(url, function(){
    		$('.ratings-place').find('form:first').find('input:first').focus();
    		$('input.star').rating();
    	});

        return false;
    });
    
    // All form actions will be submitted via AJAX
    $('body').on('click','#review input.review_button', function() {
        var $form = $(this).closest("form");
        BLC.ajax({url: $form.attr('action'), 
                type: "POST",
                data: $form.serialize()
            }, function(responseData) {
                $('.ratings-place').html(responseData);
            }
        );
        return false;
    });     
});